<?php
// Configuracion.php
class Admin_Model_Configuracion {
	private $_conn;
    
    public function __construct () {
        $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/database.ini', APPLICATION_ENV);
        $this->_conn = odbc_connect('Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=' . $config->database. ';',	
            $config->user, $config->password);
    }

    public function __destruct () {
        odbc_close($this->_conn);
    }
    
    public function getConfiguraciones() {
        $sql = <<<SQL
                SELECT * FROM CONFIGURACION
SQL;

        $resultSet = odbc_exec($this->_conn, $sql);

        $_return = array();

        while ($renglon = odbc_fetch_array($resultSet)) {
            $_return[] = array(
                'ID' => $renglon['Id'],
                'BASE_DE_DATOS' => $renglon['BASE_DE_DATOS'],
                'USAR' => intval($renglon['USAR']),
                'EDITABLE' => intval($renglon['EDITABLE'])
            );
        }
        
        return $_return;
    }
    public function getConfiguracion($id) {
        $sql = <<<SQL
            SELECT * FROM CONFIGURACION WHERE ID = $id
SQL;

        $resultSet = odbc_exec($this->_conn, $sql);

        $renglon = odbc_fetch_array($resultSet);

        $_return['ID'] = $renglon['Id'];
        $_return['BASE_DE_DATOS'] = $renglon['BASE_DE_DATOS'];
        $_return['USAR'] = intval($renglon['USAR']);
        $_return['EDITABLE'] = intval($renglon['EDITABLE']);

        return $_return;
    }
    
    public function cambiarConfiguracion($id) {
        $sql = <<<SQL
            UPDATE configuracion SET USAR = 0
SQL;
        odbc_exec($this->_conn, $sql);

        $sql = <<<SQL
            UPDATE configuracion SET USAR = 1 WHERE id = $id
SQL;

        odbc_exec($this->_conn, $sql);
    }
}
